package org.jks.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.excel.FlightLineUtil;
import org.jks.model.FlightLine;
import org.jks.request.model.Ids;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.FlightAngleService;
import org.jks.service.FlightLineService;
import org.jks.service.TowerCoordinateService;
import org.jks.util.FileUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/flight_line")
public class FlightLineController {
	@Autowired
	FlightLineService flightLineService;
	@Autowired
	TowerCoordinateService towerCoordinateService;
	@Autowired
	FlightAngleService flightAngleService;

	@RequestMapping("/add")
	public void add(FlightLine flightLine, HttpServletRequest request,
			HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		try {
			flightLineService.save(flightLine);
			ar.setSuccess(true);
			ar.setData(flightLine);
		} catch (Exception e) {
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request, HttpServletResponse response,HttpSession session){
		ActionResult ar = new ActionResult();
		try{
			flightLineService.delete(ids.getIds());
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			flightLineService.delete(id);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/update")
	public void update(FlightLine flightLine, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			flightLineService.update(flightLine);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		String[] limit = request.getParameterValues("limit");
		String[] pages = request.getParameterValues("page");
		int pagesize = 25;
		int page = 1;
		if(limit.length > 0){
			pagesize = Integer.valueOf(limit[0]);
		}
		if(pages.length > 0){
			page = Integer.valueOf(pages[0]);
		}
		try{
			long total = flightLineService.count();
			List<FlightLine> flightLines = flightLineService.list(page, pagesize);
			for(FlightLine flightLine: flightLines){
				flightLine.setFlightAngles(null);
				flightLine.setTowerCoordinates(null);
			}
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setTotal(total);
			table.setRows(flightLines);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/import")
	public void importFlightLine(@RequestParam(value = "file") MultipartFile file,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		ActionResult ar = new ActionResult();
		FlightLineUtil.importExcel(flightLineService, towerCoordinateService, flightAngleService, file, ar);
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/download")
	public void downloadTemplate(HttpServletRequest request, HttpServletResponse response, HttpSession session){
		String basePath = request.getServletContext().getRealPath("/");
		String filename = "某某线.xlsx";
		String path = basePath + File.separator + "templates" + File.separator + "export_template" + File.separator + filename;
		FileUtil.download(path, filename, response);
	}
}
